package com.example.demo.controller;

import org.springframework.stereotype.Component;

import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@ServerEndpoint("/websocket/{pageCode}")
@Component
public class WebSocketController {
    private static final String loggerName = WebSocketController.class.getName();
    public static Map<String, List<Session>> electricScoketmap = new ConcurrentHashMap<String, List<Session>>();

    //建立连接
    @OnOpen
    public void onOpen(@PathParam("pageCode") String pageCode, Session session) {
        List<Session> sessions = electricScoketmap.get(pageCode);
        if (null == sessions) {
            List<Session> sessionList = new ArrayList<>();
            sessionList.add(session);
            electricScoketmap.put(pageCode, sessionList);
        } else {
            sessions.add(session);
        }
    }

    //收到调用的信息
    @OnMessage
    public void onMessage(String message, Session session) {
        System.out.println("接收到的信息" + message);
        try {
            session.getBasicRemote().sendText("我推送的消息是" + message);
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }


    @OnError
    public void onError(Session session, Throwable throwable) {
        System.out.println("发生错误");

    }

    @OnClose
    public void onClose(@PathParam("pageCode") String pageCode, Session session) {
        if (electricScoketmap.containsKey(pageCode)) {
            electricScoketmap.get(pageCode).remove(session);
        }
    }

}
